//=============================================================================
//// Shader uses position and texture
//=============================================================================
SamplerState samLinear
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Mirror;
    AddressV = Mirror;
};
BlendState noblend
{
	BlendEnable[0] = FALSE;
	SrcBlend = SRC_COLOR;
	DestBlend = INV_SRC_ALPHA;
	BlendOp = ADD;
	SrcBlendAlpha = ZERO;
	DestBlendAlpha = ZERO;
	BlendOpAlpha = ADD;
	RenderTargetWriteMask[0] = 0x0F;
};
float BlurDistance = 0.001f;

float Threshold = 0.3;

// Controls the Intensity of the bloom texture
float BloomIntensity = 1.3;

// Controls the Intensity of the original scene texture
float OriginalIntensity = 1.0;

// Saturation amount on bloom
float BloomSaturation = 1.0;

// Saturation amount on original scene
float OriginalSaturation = 1.0;

Texture2D gTexture;

struct VS_INPUT_STRUCT
{
	float3 Pos : POSITION;
    float2 Tex : TEXCOORD;

};
struct PS_INPUT_STRUCT
{
	float4 Pos : SV_POSITION;
	float2 Tex : TEXCOORD1;
};
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT_STRUCT VS( VS_INPUT_STRUCT input )
{
	PS_INPUT_STRUCT output = (PS_INPUT_STRUCT)0;
	output.Pos = float4(input.Pos,1);
	output.Tex = input.Tex;

	return output;
}

float4 AdjustSaturation(float4 color, float saturation)
{
    // We define gray as the same color we used in the grayscale shader
    float grey = dot(color, float4(0.3, 0.59, 0.11, 1.0));
    
    return lerp(grey, color, saturation);
}
//--------------------------------------------------------------------------------------
// Pixel XMeshShader
//--------------------------------------------------------------------------------------
float4 PS_BLOOM( PS_INPUT_STRUCT input): SV_Target
{
	float4 bloomColor = gTexture.Sample( samLinear, input.Tex);
    bloomColor = (bloomColor - Threshold) / (1 - Threshold);

	float4 originalColor = gTexture.Sample( samLinear, input.Tex);

	// Adjust color saturation and intensity based on the input variables to the shader
	bloomColor = AdjustSaturation(bloomColor, BloomSaturation) * BloomIntensity;
	originalColor = AdjustSaturation(originalColor, OriginalSaturation) * OriginalIntensity;

	// make the originalColor darker in very bright areas, avoiding these areas look burned-out
    originalColor *= (1 - saturate(bloomColor));

    // Get the bright areas that is brighter than Threshold and return it.
    //return saturate(originalColor + bloomColor);

    float4 Color; // = saturate(originalColor + bloomColor);

    // Get the texel from ColorMapSampler using a modified texture coordinate. This
    // gets the texels at the neighbour texels and adds it to Color.
	Color  = gTexture.Sample( samLinear, float2(input.Tex.x+BlurDistance, input.Tex.y+ BlurDistance));
	Color += gTexture.Sample( samLinear, float2(input.Tex.x-BlurDistance, input.Tex.y-BlurDistance));
	Color += gTexture.Sample( samLinear, float2(input.Tex.x+BlurDistance, input.Tex.y-BlurDistance));
	Color += gTexture.Sample( samLinear, float2(input.Tex.x-BlurDistance, input.Tex.y+BlurDistance));
    // We need to devide the color with the amount of times we added
    // a color to it, in this case 4, to get the avg. color
    Color = Color / 4;
 
    // returned the blurred color
    return Color;

	//float4 rgb = gTexture.Sample( samLinear, input.Tex ) ;
	//float mean = ( rgb.r + rgb.g + rgb.b ) / 3.0f;
    //return float4( mean, mean, mean, 1.0f );
}
float4 PS_BLUR( PS_INPUT_STRUCT input): SV_Target
{

	//float fog = CalcFogFactor( input.fogDist );

    float4 Color;
    // Get the texel from ColorMapSampler using a modified texture coordinate. This
    // gets the texels at the neighbour texels and adds it to Color.
	Color  = gTexture.Sample( samLinear, float2(input.Tex.x+BlurDistance, input.Tex.y+ BlurDistance));
	Color += gTexture.Sample( samLinear, float2(input.Tex.x-BlurDistance, input.Tex.y-BlurDistance));
	Color += gTexture.Sample( samLinear, float2(input.Tex.x+BlurDistance, input.Tex.y-BlurDistance));
	Color += gTexture.Sample( samLinear, float2(input.Tex.x-BlurDistance, input.Tex.y+BlurDistance));
    // We need to devide the color with the amount of times we added
    // a color to it, in this case 4, to get the avg. color
    Color = Color / 4;
 
    // returned the blurred color
	return Color;
    //return fog * Color + (1.0 - fog)*g_fogColor;
}
technique10 Grayscale
{
	pass P0
    {          
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS_BLUR() ) );
		SetBlendState(noblend, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xffffffff);
    }
}

